<?php

namespace addons\shopro\controller\second;

use think\Db;
use addons\shopro\controller\Common;
use app\common\library\CheckID;
use app\admin\model\shopro\third\Patient as PatientModel;

class Patient extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $user = auth_user();

        $patients = PatientModel::where('user_id', $user->id)
            ->order('createtime', 'asc')
            ->select();

        $this->success('获取成功', $patients);
    }


    /**
     * 添加就诊人
     */
    public function add()
    {
        $user = auth_user();

        $params = $this->request->only([
            'real_name', 'id_card', 'sex', 'phone', 'age'
        ]);


        $params['user_id'] = $user->id;
        $this->svalidate($params, ".add");

        // 判断是否实名通过
        $realName = $params['real_name'];
        $idCard = $params['id_card'];
        $checkRet = CheckID::CheckNameAndID($realName, $idCard);
        if ($checkRet['code'] !== 0) {
            $this->error($checkRet['msg']);
        }
        // 判断身份证号在同一用户下是否有重复的就诊人
        $patient = PatientModel::where('user_id', $user->id)->where('id_card', $idCard)->find();
        if ($patient) {
            $this->error('就诊人身份信息重复');
        }


        Db::transaction(function () use ($params) {
            $patient = new PatientModel();
            $patient->save($params);

        });

        $this->success('保存成功');
    }

    /**
     * 就诊人详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail()
    {
        $user = auth_user();
        $id = $this->request->param('id');

        $patient = PatientModel::where('user_id', $user->id)->where('id', $id)->find();
        if (!$patient) {
            $this->error(__('No Results were found'));
        }

        $this->success('获取成功', $patient);
    }


    /**
     * 编辑就诊人
     *
     * @return \think\Response
     */
    public function edit()
    {
        $user = auth_user();
        $id = $this->request->param('id');

        $patient = PatientModel::where('user_id', $user->id)->where('id', $id)->find();
        if (!$patient) {
            $this->error(__('No Results were found'));
        }

        $params = $this->request->only([
            'real_name', 'id_card', 'sex', 'phone', 'age'
        ]);
        $this->svalidate($params, ".edit");

        $realName = $params['real_name'];
        $idCard = $params['id_card'];
        if ($realName != $patient->real_name || $idCard != $patient->id_card) {
            $checkRet = CheckID::CheckNameAndID($realName, $idCard);
            if ($checkRet['code'] !== 0) {
                $this->error($checkRet['msg']);
            }
        }
        // 如果身份证号修改过，判断身份证在本用户下添加过
        if ($idCard != $patient->id_card) {
            // 判断身份证号在同一用户下是否有重复的就诊人
            $oldPatient = PatientModel::where('user_id', $user->id)->where('id_card', $idCard)->find();
            if ($oldPatient) {
                $this->error('就诊人身份信息重复');
            }
        }


        Db::transaction(function () use ($params, $patient) {
            $patient->save($params);
        });

        $this->success('保存成功');
    }


    /**
     * 删除就诊人
     *
     * @param string $id 要删除的
     * @return void
     */
    public function delete()
    {
        $user = auth_user();
        $id = $this->request->param('id');

        $patient = PatientModel::where('user_id', $user->id)->where('id', $id)->find();
        if (!$patient) {
            $this->error(__('No Results were found'));
        }

        $patient->delete();

        $this->success('删除成功');
    }
}
